package com.greatwall.hip.cms.configuration;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author TianLei
 * @version V1.0
 */
@Configuration
@Data
@EnableSwagger2
@EnableKnife4j
@ConfigurationProperties(prefix = "swagger")
@PropertySource(value="classpath:swagger.properties", encoding="UTF-8")
public class Knife4jSwaggerConfiguration {

    private Boolean enabled;

    private String docUrl;

    private String serviceUrl;

    private String title;

    private String description;

    private String version;

    private String groupName;

    private String pkgName;

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .enable(enabled)
                .apiInfo(apiInfo())
                //分组名称
                .groupName(groupName)
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(pkgName))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 设置文档信息主页的内容说明
     *
     * @return 文档信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .termsOfServiceUrl(serviceUrl)
                .contact(new Contact("互联网医院后端开发", docUrl, null))
                .license("what")
                .version(version)
                .build();
    }
}
